package lucene.server.database.utils;


import lucene.server.database.meta.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author jinji
 */
public class DialectUtil {

    public static Connection getConnection(Properties properties){
        String databaseType = properties.getProperty("type").toLowerCase();
        try {
            // 加载并注册JDBC驱动类
            Class.forName(getDriver(databaseType));
            // 建立数据库连接
            Connection conn = DriverManager.getConnection(getUrl(databaseType, properties), properties.getProperty("username"),  properties.getProperty("password"));
            return conn;
        }catch (Exception e){
            return null;
        }
    }
    public static String getDatabaseSql(String databaseType){
        switch (databaseType.toLowerCase()){
            case "mysql":
                return "SHOW DATABASES";
            case "oracle":
                return "SELECT name FROM v$database";
            case "postgresql":
                return "SELECT datname FROM pg_database";
            case "sqlserver":
                return "SELECT name FROM sys.databases";
            default:
                return null;
        }
    }
    public static String getDriver(String databaseType){
        switch (databaseType.toLowerCase()){
            case "mysql":
                return "com.mysql.cj.jdbc.Driver";
            case "oracle":
                return "oracle.jdbc.driver.OracleDriver";
            case "postgresql":
                return "org.postgresql.Driver";
            case "sqlserver":
                return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            default:
                return null;
        }
   }
    public static String getUrl(String databaseType, Properties properties){
        switch (databaseType.toLowerCase()){
            case "mysql":
                return "jdbc:mysql://" + properties.getProperty("host") + ":" + properties.getProperty("port") + "/?useInformationSchema=true&nullCatalogMeansCurrent=false&useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL";
            case "oracle":
                return "jdbc:oracle:thin:@" + properties.getProperty("host") + ":" + properties.getProperty("port");
            case "postgresql":
                return "jdbc:postgresql://" + properties.getProperty("host") + ":" + properties.getProperty("port") ;
            case "sqlserver":
                return "jdbc:sqlserver://" + properties.getProperty("host") + ":" + properties.getProperty("port") + ";";
            default:
                return null;
        }
   }


    public static IDialectMeta createDialectMeta(String  databaseType){

        switch (databaseType.toLowerCase()){
            case "mysql":
                return  new MysqlDialect(databaseType);
            case "oracle":
                return new OracleDialect(databaseType);
            case "postgresql":
                return new PostgreSqlDialect(databaseType);
            case "sqlserver":
                return new SqlServerDialect(databaseType);
            default:
                return new AbstractDialect(databaseType);
        }
    }
    public static void setFetchSize(PreparedStatement ps,String  databaseType) throws SQLException {

        switch (databaseType.toLowerCase()){
            case "mysql":
                ps.setFetchSize(Integer.MIN_VALUE);
                break;
            case "oracle":
                ps.setFetchSize(10000);
                break;
            case "postgresql":
                ps.setFetchSize(10000);
                break;
            case "sqlserver":
                ps.setFetchSize(Integer.MAX_VALUE - 1);
                break;
            default:
                ps.setFetchSize(10000);
                break;
        }
    }

}
